package cn.learn.controller.cargo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.*;
import cn.learn.service.cargo.ContractService;
import cn.learn.service.cargo.ExportProductService;
import cn.learn.service.cargo.ExportService;
import cn.learn.vo.ExportProductVo;
import cn.learn.vo.ExportResult;
import cn.learn.vo.ExportVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {

    @Reference
    private ContractService contractService;
    @Reference
    private ExportService exportService;
    @Reference
    private ExportProductService exportProductService;

    /**
     * 进入合同管理页面(已上报)
     *
     * @return
     */
    @RequestMapping(value = "contractList", name = "合同管理页面")
    public String contractList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        ContractExample contractExample = new ContractExample();
        // 设置排序查找
        contractExample.setOrderByClause("create_time desc");
        // 设置条件
        ContractExample.Criteria criteria = contractExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andStateEqualTo(1);
        // 查询
        PageInfo pageList = contractService.findAll(contractExample, pageNum, pageSize);
        request.setAttribute("pageList", pageList);
        return "/cargo/export/export-contractList";
    }

    /**
     * 生成报运单 与 到生成报运单页面
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "toExport", name = "生成报运单")
    public String toExport(String id) {
        request.setAttribute("id", id);
        return "/cargo/export/export-toExport";
    }

    /**
     * 生成报运单 与 到生成报运单页面
     *
     * @param export
     * @return
     */
    @RequestMapping(value = "makeExport", name = "生成报运单")
    public String makeExport(Export export) {
        export.setCompanyId(companyId);
        export.setCompanyName(companyName);
        System.out.println(export);
        exportService.save(export);
        return "redirect:/cargo/export/list.do";
    }

    /**
     * 报运单页面
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list", name = "出口报运管理页面")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        ExportExample exportExample = new ExportExample();
        exportExample.setOrderByClause("create_time desc");
        ;
        ExportExample.Criteria criteria = exportExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo pageList = exportService.findAll(exportExample, pageNum, pageSize);
        request.setAttribute("pageList", pageList);
        return "/cargo/export/export-list";
    }

    /**
     * 报运单修改
     *
     * @return
     */
    @RequestMapping(value = "/updateOne", name = "跳转到报运单修改页面")
    public String updateOne(Export export) {
        if (request.getMethod().equals("POST")) {
            export.setCompanyId(companyId);
            export.setCompanyName(companyName);
            exportService.update(export);
        }
        Export resultExport = exportService.findById(export.getId());
        request.setAttribute("export", resultExport);
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(export.getId());
        List<ExportProduct> list = exportProductService.findAll(exportProductExample);
        request.setAttribute("eps", list);
        return "/cargo/export/export-update";
    }

    /**
     * 跳转到报运单查看页面
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/toView", name = "到报运单的查看页面")
    public String toView(String id) {
        Export export = exportService.findById(id);
        request.setAttribute("export", export);
        return "/cargo/export/export-view";
    }

    /**
     * 删除报运单
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", name = "报运单删除")
    public String delete(String id) {
        exportService.delete(id);
        return null;
    }

    @RequestMapping(value = "/exportE", name = "海关报运")
    public String exportE(String id) {
        // 根据报运单id获取报运单对象
        Export export = exportService.findById(id);
        // 组装exportVo
        ExportVo exportVo = new ExportVo();
        BeanUtils.copyProperties(export, exportVo);
        exportVo.setExportId(export.getId());
        // 获取报运单的货物对象
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> productList = exportProductService.findAll(exportProductExample);
        // 组装
        List<ExportProductVo> list = new ArrayList();
        if (productList != null) {
            for (ExportProduct exportProduct : productList) {
                ExportProductVo exportProductVo = new ExportProductVo();
                BeanUtils.copyProperties(exportProduct, exportProductVo);
                exportProductVo.setExportProductId(exportProduct.getId());
                // 组装成集合
                list.add(exportProductVo);
            }
        }
        // 将exportProductVo封装给exportVo
        exportVo.setProducts(list);
        // 根据指定的提交方式调用服务指定的参数
        WebClient webClient = WebClient.create("请求地址");
        webClient.post(exportVo);
        // 做查询，根据返回内容修改自己的内容
        WebClient webClient1 = WebClient.create("请求地址+参数");
        ExportResult exportResult = webClient1.get(ExportResult.class);
        // 修改自己的报运单参数(状态与备注)，以及报运单货物数据(税值)
        exportService.updateByResult(exportResult);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "toPacking", name = "到生成装箱单界面")
    public String toPacking(String id) {
        request.setAttribute("id", id);
        return "/cargo/packing/packing-toPacking";
    }

    @RequestMapping(value = "submit", name = "提交报运单状态为2")
    public String submit(String id) {
        Export export = exportService.findById(id);
        export.setState(2);
        exportService.submit(export);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "cancel", name = "取消报运单状态")
    public String cancel(String id) {
        Export export = exportService.findById(id);
        export.setState(0);
        exportService.submit(export);
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping(value = "exportP", name = "查询报运单状态并返回")
    @ResponseBody
    public ArrayList exportP(String id) {
        String[] ids = id.split(",");
        ArrayList list = new ArrayList();
        for (int i = 0; i < ids.length; i++) {
            Export export = exportService.findById(ids[i]);
            list.add(export.getState());
        }
        return list;
    }

    /**
     * 跳转到地图
     * @param id
     * @return
     */
    @RequestMapping("/exportMap")
    public String exportMap(String id) {
        Export export = exportService.findById(id);
        String placeDeparture = export.getShipmentPort();
        String destination = export.getDestinationPort();
        request.setAttribute("placeDeparture", placeDeparture);
        request.setAttribute("destination", destination);

        return "cargo/export/export_Map";
    }

}